## Read in the data
### Oil spills by county
ca_oil_spills <- read_sf(here("data", "oil_spills", "Oil_Spill_Incident_Tracking_%5Bds394%5D.shp")) %>% 
  clean_names() %>% 
  rename(object_id = objectid,
         dfg_control = dfgcontrol,
         oes_number = oesnumber,
         date = dateofinci,
         inland_marine = inlandmari,
         time = timeofinci,
         location = specificlo,
         city = localecity,
         county_name = localecoun) 

  
### California counties shape file
ca_counties <- read_sf(here("data", "ca_counties", "CA_Counties_TIGER2016.shp")) %>% 
  clean_names() %>% 
  dplyr::select(name) %>% 
  rename(county_name = name)
## Oil spill data wrangling
ca_oil_spills_tidy <- ca_oil_spills %>%
  mutate(date = as.Date(date)) %>% 
  mutate(date = ymd(date)) %>% 
  mutate(year = year(date)) %>% 
  dplyr::select(date, year, longitude, latitude, city, county_name, inland_marine)

Interactive map of California oil spills in 2008

tmap_mode("view")

tm_shape(ca_counties) +
  tm_fill("county_name", palette = "Greens", title = "County") +
  tm_shape(ca_oil_spills_tidy) +
  tm_dots(size = 0.03, col = "darkorange2", border.col = "black") +
  tm_view(view.legend.position = c("right", "top"))

Figure 1: Interactive plot with the exact locations where oil spills occurred in California in 2008. Data source: https://gis.data.ca.gov/datasets/7464e3d6f4924b50ad06e5a553d71086_0/data

California oil spills by county

## Part 2
## Counts of California oil spills by county
ca_oil_spills_join <- ca_counties %>% 
  st_join(ca_oil_spills_tidy)

oil_spills_count <- ca_oil_spills_join %>% 
  filter(inland_marine %in% c("Inland")) %>% 
  count(county_name.x, inland_marine) %>%
  mutate(n = as.numeric(n))


ggplot(data = oil_spills_count) +
  geom_sf(aes(fill = n),
          color = "black",
          size = 0.5) +
  scale_fill_viridis_c() +
  labs(title = "Inland oil spill records by county in California for 2008",
       fill = "Number of oil spill records") +
  theme_bw() +
  theme(axis.text = element_text(size = 10, face = "bold", color = "black"),
        panel.grid = element_blank())

Figure 2: Choropleth map of the total inland oil spills in California by county in 2008. Data source: https://gis.data.ca.gov/datasets/7464e3d6f4924b50ad06e5a553d71086_0/data